package com.onesports.intelligent.k12.polarlight.util;

import static com.onesports.intelligent.k12.polarlight.common.utils.DateUtils.HH_MM;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.onesports.intelligent.k12.polarlight.common.utils.DateUtils;

/**
 * 获取重叠时段数据
 *
 * @author clw
 * @date 2022-07-27 15:30:10
 */
public class TimeSlotUtil {

    public static void main(String[] args) {
        Map<String, String> map1 = new HashMap<>();
        map1.put("startTime", "09:00");
        map1.put("endTime", "10:00");

        Map<String, String> map2 = new HashMap<>();
        map2.put("startTime", "09:30");
        map2.put("endTime", "10:00");

        Map<String, String> map3 = new HashMap<>();
        map3.put("startTime", "10:10");
        map3.put("endTime", "10:33");

        Map<String, String> map4 = new HashMap<>();
        map4.put("startTime", "10:30");
        map4.put("endTime", "11:30");

        List<Map<String, String>> list = new ArrayList<>();
        //list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);

        List<Map<String, String>> result = getMaps(list,map1);
        System.out.print(result.toString());
    }

    public static List<Map<String, String>> getMaps(List<Map<String, String>> list,Map<String, String> item1) {
        List<Map<String, String>> result = new ArrayList<>();

        // 外层判断是比对所有
        //for (Map<String, String> item1 : list) {
        //    if (result.contains(item1)) {
        //        continue;
        //    }
            for (Map<String, String> item2 : list) {
                if (result.contains(item2)) {
                    continue;
                }
                if (Objects.equals(item1, item2)) {
                    continue;
                }
                Date start1 = DateUtils.dateTime(HH_MM, item1.get("startTime"));
                Date end1 = DateUtils.dateTime(HH_MM, item1.get("endTime"));
                Date start2 = DateUtils.dateTime(HH_MM, item2.get("startTime"));
                Date end2 = DateUtils.dateTime(HH_MM, item2.get("endTime"));

                if (!(start1.getTime() >= end2.getTime() || end1.getTime() <= start2.getTime())) {
                    //result.add(item1);
                    result.add(item2);
                    break;
                }
            }
        //}
        return result;
    }
}
